package br.ufrj.dcc.smarthotel.activities;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.Util;
import br.ufrj.dcc.smarthotel.controls.EventDetailControl;
import br.ufrj.dcc.smarthotel.entities.Event;
import br.ufrj.dcc.smarthotel.entities.EventItem;
import br.ufrj.dcc.smarthotel.entities.EventUser;
import br.ufrj.dcc.smarthotel.entities.User;
import br.ufrj.dcc.smarthotel.listeners.EventDetailClickListener;

public class EventDetailActivity extends AppActivity {
	private EventDetailControl control;
	private EventDetailClickListener listener;
	private boolean isAttendant = false;
	
	private Event clickedEvent;
	private List<EventUser> attendantsList;
	private List<EventItem> itemsList;
	
	private ImageView imgEventIcon;
	private TextView txtEventTitle;
	private TextView txtEventDate;
	private TextView txtEventNumberOfPeople;
	private TextView txtEventDescription;
	private Button btnConfirmAttendant;
	private Button btnInvitePeople;
	private ListView listEventItems;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        int clickedEventId = Integer.parseInt(this.getIntent().getExtras().getString("clickedEventId"));
        
        setContentView(R.layout.eventdetail);
        
        this.findAllViews();
        this.control = new EventDetailControl(this);
        this.listener = new EventDetailClickListener(this);
        
        this.control.loadEventDetails(clickedEventId);
    }
	
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		//int clickedEventId = Integer.parseInt(data.getExtras().getString("clickedEventId"));
		
		this.control.loadEventDetails(this.clickedEvent.getEventId());
	}

	private void findAllViews() {
		this.imgEventIcon = (ImageView) this.findViewById(R.id.imgEventIcon);
		this.txtEventTitle = (TextView) this.findViewById(R.id.txtEventTitle);
		this.txtEventDate = (TextView) this.findViewById(R.id.txtEventDate);
		this.txtEventNumberOfPeople = (TextView) this.findViewById(R.id.txtEventNumberOfPeople);
		this.txtEventDescription = (TextView) this.findViewById(R.id.txtEventDescription);
		this.btnConfirmAttendant = (Button) this.findViewById(R.id.btnConfirmAttendant);
		this.btnInvitePeople = (Button) this.findViewById(R.id.btnInvitePeople);
		this.listEventItems = (ListView) this.findViewById(R.id.listEventItems);
	}
	
	public void setClickedEvent(Event event) {
		this.clickedEvent = event;
		
		this.txtEventTitle.setText(this.clickedEvent.getName());
		this.txtEventDate.setText(Util.getInstance(this.getApplicationContext()).writeDateTime(this.clickedEvent.getStartDate()));
		this.txtEventDescription.setText(this.clickedEvent.getDescription());
		this.imgEventIcon.setImageBitmap(Util.getInstance(this.getApplicationContext()).downloadImage(this.clickedEvent.getPictureUrl()));
	}
	
	public void setAttendantsList(List<EventUser> attendantsList) {
		this.attendantsList = attendantsList;
		
		int numberOfAttendants = this.attendantsList == null ? 0 : this.attendantsList.size();
		
		this.txtEventNumberOfPeople.setText(this.getString(R.string.lbl_NumberOfAttendants, numberOfAttendants));
		
		User loggedUser = AppActivity.getSessionInfo().getUser();
		
		if (attendantsList != null) {
			for (EventUser attendant : attendantsList) {
				if (attendant.getUser().getUserId() == loggedUser.getUserId()) {
					this.isAttendant = true;
					
					break;
				}
			}
		}
		
		if (this.isAttendant) {
			this.btnConfirmAttendant.setText(R.string.msg_UserConfirmed);
			this.btnInvitePeople.setVisibility(View.VISIBLE);
		}
		else {
			this.btnInvitePeople.setVisibility(View.INVISIBLE);
		}
		
		this.btnConfirmAttendant.setOnClickListener(this.listener);
		this.btnInvitePeople.setOnClickListener(this.listener);
	}

	public void setItemsList(List<EventItem> itemsList) {
		this.itemsList = itemsList;
		
		if (itemsList != null && itemsList.size() > 0) {
			this.listEventItems.setAdapter(new EventItemsListAdapter(this.getApplicationContext(), R.id.listEventItems, this.itemsList));
			this.listEventItems.setVisibility(View.VISIBLE);
		}
		else {
			this.listEventItems.setVisibility(View.INVISIBLE);
		}
	}
	
	public void ConfirmUser() {
		this.control.ConfirmUser();
		
		this.control.loadEventDetails(this.clickedEvent.getEventId());
	}
	
	public void UnconfirmUser() {
		//this.control.UnconfirmUser();
		
		this.showQuickMessage(R.string.msg_NotImplemented);
	}
	
	public Event getClickedEvent() {
		return this.clickedEvent;
	}
	
	public boolean userIsAttendant() {
		return this.isAttendant;
	}
	
	class EventItemsListAdapter extends ArrayAdapter<EventItem> {
		List<EventItem> eventItemsList;
		
		public EventItemsListAdapter(Context context, int textViewResourceId, List<EventItem> objects) {
			super(context, textViewResourceId, objects);
			
			this.eventItemsList = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            
	            v = vi.inflate(R.layout.eventitem_item, null);
	        }
	        
	        EventItem eventItem = eventItemsList.get(position);
	        
	        if (eventItem != null) {
	                TextView txtEventItemTitle = (TextView) v.findViewById(R.id.txtEventItemTitle);
	                TextView txtEventItemDate = (TextView) v.findViewById(R.id.txtEventItemDate);
	                
	                txtEventItemTitle.setText(eventItem.getName());
	                txtEventItemDate.setText(Util.getInstance(getContext()).writeDateTime(eventItem.getStartDate()));
	        }
	        return v;
		}
	}
}
